/************************************************************************************/
/*  Program name CAPAPIMP_PAY_FORMAT.sql                                            */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

ALTER TRIGGER AP_CHECK_FORMATS_CT1 DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAPIMP_PAY_FORMAT';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_AP_PAY_FORMAT;

	CURSOR Cur_G IS
	SELECT
		A.*,
		B.Check_Format_ID_N
	FROM
		CAP_CHECK_FORMATS		A,
		CRST_AP_PAY_FORMAT_MAP	B
	WHERE	A.Check_Format_ID	=	B.Check_Format_ID;

	lv_Next_Seq_ID		NUMBER;

BEGIN

	--  RENAME
	UPDATE
		AP_CHECK_FORMATS
	SET
		Name	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(Name,1,4),Name)	--	RENAME(NAME)
	WHERE	Check_Format_ID	IN
							(
							SELECT
								Check_Format_ID
							FROM
								CRST_AP_PAY_FORMAT
							);

	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	AP_CHECK_FORMATS_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT INTO CRST_AP_PAY_FORMAT_MAP VALUES
			(
			C1.CHECK_FORMAT_ID,
			CAPBRIMP_MAPPING.RenameStr(SUBSTR(C1.NAME,1,4),C1.NAME),
			C1.PAYMENT_METHOD_LOOKUP_CODE,
			lv_Next_Seq_ID,
			C1.NAME,
			C1.PAYMENT_METHOD_LOOKUP_CODE
			);

			INSERT INTO AP_CHECK_FORMATS
			VALUES
			(
			lv_Next_Seq_ID,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),		--LAST_UPDATED_BY,
			C1.NAME,
			C1.TYPE,
			C1.STUB_FIRST_FLAG,
			C1.CHECK_LENGTH,
			C1.STUB_LENGTH,
			C1.INVOICES_PER_STUB,
			C1.OVERFLOW_RULE,
			C1.PRE_NUMBERED_FLAG,
			C1.CHECK_NUM_ROW_LINE,
			C1.DATE_ROW_LINE,
			C1.AMOUNT_ROW_LINE,
			C1.AMOUNT_WORDS_ROW_LINE,
			C1.VENDOR_NUM_ROW_LINE,
			C1.VENDOR_NAME_ROW_LINE,
			C1.VENDOR_ADDRESS_ROW_LINE,
			C1.STUB_CHECK_NUM_ROW_LINE,
			C1.STUB_VENDOR_NAME_ROW_LINE,
			C1.STUB_VENDOR_NUM_ROW_LINE,
			C1.STUB_FIRST_INVOICE_ROW_LINE,
			C1.PAYMENT_METHOD_LOOKUP_CODE,
			C1.CURRENCY_CODE,
			C1.CREATE_PAYMENTS_PROGRAM_ID,			--  ???
			C1.CONFIRM_PAYMENTS_PROGRAM_ID,			--  ???
			C1.SEPARATE_REMITTANCE_ADVICE,
			CAPBRIMP_COMMON.GetPayProgramID(C1.REMIT_NAME),
			C1.LAST_UPDATE_LOGIN,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),			--CREATED_BY,
			C1.EFT_TYPE,
			C1.MULTI_CURRENCY_FLAG,
			C1.ATTRIBUTE_CATEGORY,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.ZERO_AMOUNTS_ONLY,
			CAPBRIMP_COMMON.GetPayProgramID(C1.BUILD_NAME),
			CAPBRIMP_COMMON.GetPayProgramID(C1.FORMAT_NAME),
			C1.PRINT_CHECK_STUB,
			C1.GROUP_BY_DUE_DATE,
			C1.GLOBAL_ATTRIBUTE_CATEGORY,
			C1.GLOBAL_ATTRIBUTE1,
			C1.GLOBAL_ATTRIBUTE2,
			C1.GLOBAL_ATTRIBUTE3,
			C1.GLOBAL_ATTRIBUTE4,
			C1.GLOBAL_ATTRIBUTE5,
			C1.GLOBAL_ATTRIBUTE6,
			C1.GLOBAL_ATTRIBUTE7,
			C1.GLOBAL_ATTRIBUTE8,
			C1.GLOBAL_ATTRIBUTE9,
			C1.GLOBAL_ATTRIBUTE10,
			C1.GLOBAL_ATTRIBUTE11,
			C1.GLOBAL_ATTRIBUTE12,
			C1.GLOBAL_ATTRIBUTE13,
			C1.GLOBAL_ATTRIBUTE14,
			C1.GLOBAL_ATTRIBUTE15,
			C1.GLOBAL_ATTRIBUTE16,
			C1.GLOBAL_ATTRIBUTE17,
			C1.GLOBAL_ATTRIBUTE18,
			C1.GLOBAL_ATTRIBUTE19,
			C1.GLOBAL_ATTRIBUTE20,
			C1.BANK_FILE_CHARACTER_SET,
			C1.TRANSMISSIONS_FLAG
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	BEGIN
		FOR C2 IN Cur_G LOOP
			INSERT INTO CAP_CHECK_FORMATS VALUES
			(
				C2.Check_Format_ID_N,
				CAPBRIMP_MAPPING.GetOrgMapping(C2.S_ORG_ID),
				CAPBRIMP_MAPPING.GetBGMapping(C2.S_BUSINESS_GROUP_ID)
			);
		END LOOP;
	EXCEPTION
		WHEN OTHERS THEN
			DBMS_OUTPUT.Put_Line(SQLERRM);
			lv_Err_Count	:=	lv_Err_Count + 1;
	END;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/

ALTER TRIGGER AP_CHECK_FORMATS_CT1 ENABLE;
